package zy.blue7.gui.netty.package1.lengthfieldbasedframedecoder;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

/**
 * @Author: blue7
 * @Date: 2022/2/25 22:34
 * @Version 1.0
 * @Description
 */
public class Server {

    public static void main(String[] args) throws InterruptedException {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {

                        /**
                         *
                         *
                         *     private final ByteOrder byteOrder;         todo  数据存储采用大端模式或小端模式
                         *     private final int maxFrameLength;         todo 发送的数据帧最大长度     发送数据包长度 = 长度域的值 + lengthFieldOffset + lengthFieldLength + lengthAdjustment
                         *     private final int lengthFieldOffset;      todo 定义长度域位于发送的字节数组中的下标。换句话说：发送的字节数组中下标为${lengthFieldOffset}的地方是长度域的开始地方
                         *     private final int lengthFieldLength;      todo 用于描述定义的长度域的长度。换句话说：发送字节数组bytes时, 字节数组bytes[lengthFieldOffset,lengthFieldOffset+lengthFieldLength]域对应于的定义长度域部分
                         *     private final int lengthFieldEndOffset;   todo
                         *     private final int lengthAdjustment;
                         *     private final int initialBytesToStrip;    todo 接收到的发送数据包，去除前initialBytesToStrip位
                         *     private final boolean failFast;           todo  true: 读取到长度域超过maxFrameLength，就抛出一个 TooLongFrameException。false: 只有真正读取完长度域的值表示的字节之后，才会抛出 TooLongFrameException，默认情况下设置为true，建议不要修改，否则可能会造成内存溢出
                         *     private boolean discardingTooLongFrame;
                         *     private long tooLongFrameLength;
                         *     private long bytesToDiscard;
                         *
                         *
                         *
                         *
                         *    public LengthFieldBasedFrameDecoder(
                         *     ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset,
                         *     int lengthFieldLength,int lengthAdjustment, int initialBytesToStrip,
                         *     boolean failFast) {
                         *     //...
                         * }
                         *
                         * StringDecoder的功能更简单，将接收到的对象转换成字符串，然后继续调用后面的handler，LineBaseFrameDecoder+StringDecoder组合就是按行切换的文本解码器，
                         * 它被设计用来支持TCP的粘包和拆包。
                         */

                        /**
                         * lengthFieldOffset   = 0
                         * lengthFieldLength   = 2
                         * lengthAdjustment    = 0
                         * initialBytesToStrip = 0 (= do not strip header)
                         *
                         * BEFORE DECODE (14 bytes)         AFTER DECODE (14 bytes)
                         * +--------+----------------+      +--------+----------------+
                         * | Length | Actual Content |----->| Length | Actual Content |
                         * | 0x000C | "HELLO, WORLD" |      | 0x000C | "HELLO, WORLD" |
                         * +--------+----------------+      +--------+----------------+
                         *
                         * todo 满足这种形式，  只有 长度域和数据域，不会截取长度域，， 传到下一个 handler 时， 数据还包含 长度域
                         *
                         */
                        new LengthFieldBasedFrameDecoder(1000, 0, 2);

                        /**
                         * lengthFieldOffset   = 0
                         * lengthFieldLength   = 2
                         * lengthAdjustment    = 0
                         * initialBytesToStrip = 2 (= the length of the Length field)
                         *
                         * BEFORE DECODE (14 bytes)         AFTER DECODE (12 bytes)
                         * +--------+----------------+      +----------------+
                         * | Length | Actual Content |----->| Actual Content |
                         * | 0x000C | "HELLO, WORLD" |      | "HELLO, WORLD" |
                         * +--------+----------------+      +----------------+
                         *
                         * todo 满足这种形式，  只有 长度域和数据域， 会截取长度域，， 传到下一个 handler 时， 数据 不包含 长度域
                         *
                         * https://www.iteye.com/blog/bylijinnan-1985706
                         *
                         */
                        new LengthFieldBasedFrameDecoder(1000, 0, 2,0,2);
//                        socketChannel.pipeline().addLast(new LengthFieldBasedFrameDecoder(6));
                        /**
                         * 将 bytebuf 转换成 string
                         */
                        socketChannel.pipeline().addLast(new StringDecoder());
                        socketChannel.pipeline().addLast(new TimeServerHandler());
                    }
                })
                .bind(8080).sync();
    }
}
